
<template>
  <div class="company-item">
    <Collapse simple v-model="valueCollapse" @on-change="onChange">
      <Panel name="1" hide-arrow>
        <div class="company-bg">
          <div class="company-title" @mouseenter="onMouseenter($event,info)" @mouseleave="onMouseleave">
            <p class="title">{{ info.companyName }}</p>
            <p class="nowrap">（项目: {{info.projectCount}} / 总数: {{info.projectTotalCount}}）</p>
          </div>
          <BtnText type="primary" @BtnClick="onView(info)">查看详情</BtnText>
          <Icon v-if="isDropdown" type="md-arrow-dropdown" size="30" />
          <Icon v-else type="md-arrow-dropup" size="30" />
        </div>
        <p slot="content">
          <Tree
            v-if="info.childrenCompanyList && info.childrenCompanyList.length"
            :data="info.childrenCompanyList"
            :render="renderContent"
            expand-node
            children-key="childrenCompanyList"
          ></Tree>
        </p>
      </Panel>
    </Collapse>
  </div>
</template>

<script>

export default {
  name: 'companyItem',
  props: {
    info: {
      type: Object,
      default: () => { }
    },
  },
  data () {
    return {
      treeData: [],
      isDropdown: true,
      valueCollapse: '1'
    }
  },
  methods: {
    onSelect (e) {
      console.log(e);
    },
    renderContent (h, { root, node, data }) {
      return h('div', {
        style: {
          display: 'flex',
          'justify-content': 'space-between',
          'align-items': 'center',
          'padding-top': '2px'
        }
      }, [
        h('div', {
          style: {
            display: 'flex',
            'align-items': 'center',
            width: '85%'
          },
          on: {
            mouseenter: (e) => {
              this.onMouseenter(e, data)
            },
            mouseleave: () => {
              this.onMouseleave()
            }
          }
        }, [
          h('p', {
            style: {
              'max-width': '260px',
              'font-weight': 500,
              'text-overflow': 'ellipsis',
              'overflow': 'hidden',
              'white-space': 'nowrap',
            },
          }, data.companyName),
          h('p', {
            style: {
              'white-space': 'nowrap',
            },
          },
            data.projectCount === data.projectTotalCount ? `（项目: ${data.projectCount}）` : `（项目: ${data.projectCount} / 总数: ${data.projectTotalCount}）`),
        ]),
        h('BtnText', {
          props: {
            type: 'primary'
          },
          on: {
            BtnClick: () => {
              this.onView(data)
            }
          }
        }, '查看详情')
      ]);
    },
    onChange (arr) {
      this.isDropdown = !arr.length
    },
    onView (info) {
      this.$emit('view', info)
    },
    onMouseenter (e, data) {
      this.$emit('showDetails', data, e.clientY)
    },
    onMouseleave () {
      this.$emit('hideDetails')
    },
  }
}
</script>

<style lang='less' scoped>
.company-item {
  position: relative;
  z-index: 99;
  .title {
    max-width: 270px;
    font-size: 20px;
    font-weight: 500;
    justify-content: space-between;
    align-items: center;
    text-overflow: ellipsis; /* 溢出显示省略号 */
    overflow: hidden; /* 溢出隐藏 */
    white-space: nowrap; /* 强制不换行 */
  }

  .nowrap {
    white-space: nowrap; /* 强制不换行 */
  }

  .company-bg {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: 3px;
    &:hover {
      background-color: #e5f1fe;
    }
  }

  .company-title {
    display: flex;
    align-items: center;
    width: 370px;
  }
}
</style>
<style lang="less">
.company-item {
  .ivu-collapse {
    border: none;
    .ivu-collapse-header {
      padding-left: 0;
    }

    .ivu-collapse-content {
      padding: 0;
    }
  }

  .ivu-tree-title {
    width: 100%;
  }
}
</style>
